package top.hundreds.oasys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.crud.service.BaseService;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Matter;
import top.hundreds.oasys.bean.enums.MatterStatus;
import top.hundreds.oasys.bean.vo.MatterVo;
import top.hundreds.oasys.mapper.MatterMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Matter)表服务实现类
 *
 * @author lcf
 * @since 2021-01-29 19:15:52
 */
@Service("matterService")
public class MatterService extends BaseService<Matter, MatterMapper> {

    @Resource
    private Converter<Matter,MatterVo> matterVoBuilder;

    public void setStatus(Long id, Integer status) {
        if(MatterStatus.formValue(status) == MatterStatus.UNFINISHED) {
            this.mapper.setStatus(id,status,null);
        }else {
            this.mapper.setStatus(id,status,new Date());
        }
    }

    public MatterVo buildVo(Matter matter) {
        MatterVo matterVo = new MatterVo();
        matterVoBuilder.buildVo(matter,matterVo);
        matterVo.setStatusName(MatterStatus.formValue(matterVo.getStatus()).getLabel());
        return matterVo;
    }

    public List<MatterVo> list(PageParam<Matter> pageParam) {
        Page<Matter> page = getPage(pageParam);
        if(page.getRecords().size() == 0) {
            return null;
        }
        List<Matter> records = page.getRecords();
        List<MatterVo> list = new ArrayList<>(records.size());
        for (Matter record : records) {
            list.add(buildVo(record));
        }
        return list;
    }
}
